﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BSC_OTC_Management.BSC_SERVICES;

namespace BSC_OTC_Management
{
    public partial class FrmMain : Form
    {

        #region Khai báo biến
        BSC_SERVICES.Service1SoapClient _Services_BSC_OTC = new BSC_SERVICES.Service1SoapClient();
        #endregion

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            #region thêm event vào form
            this.FormClosed += new FormClosedEventHandler(FrmMain_FormClosed);
            #endregion

            #region Load giao diện
            // hien menu left
            UserControls.MenuLeft _menuLeft = new UserControls.MenuLeft(this.splitContainer_Main.Panel1.Width, this.splitContainer_Main.Panel1.Height);
            this.splitContainer_Main.Panel1.Controls.Add(_menuLeft);

            //hiển thị phần duyệt giao dịch
            UserControls.Transaction_Approval _ta = new UserControls.Transaction_Approval(this.splitContainer_Main.Panel2.Width, this.splitContainer_Main.Panel2.Height);
            this.splitContainer_Main.Panel2.Controls.Add(_ta);

            // hien ngay gio he thong
            this.ShowDatetime();

            // hien thi nguoi dung
            this.ShowCurrentUser();
            
            // Show version he thong
            this.ShowVersion();
            #endregion
        }

        #region load giao diện

        private void ShowVersion()
        {
            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
            {
                this.Text = ".:: OTCManagement " + 
                     System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(4) + 
                     " On " + DateTime.Now.ToShortDateString() + " ::.";
            }
        }

        private void ShowDatetime()
        {
            this.slb_currentDate.Text = DateTime.Today.ToShortDateString();
        }

        private void ShowCurrentUser()
        {
            this.slb_currentUser.Text = Code.StaticVariable.username;
        }
       
        #endregion

        #region load Event

        // Enter textbox giao dich
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtMaGiaoDich.Text.Trim().Length>0)
                {
                    DataTable _result = this.tableCmdMenu(this.txtMaGiaoDich.Text.ToUpper());
                    if (_result.Rows.Count>0)
                    {
                        string menuID = _result.Rows[0]["MENUID"].ToString();
                        string autoid = _result.Rows[0]["AUTOID"].ToString();
                        DataRow rowMenu = _Services_BSC_OTC.GetMenu_ByMenuID(menuID).Rows[0];
                        string function_name = rowMenu["MENUNAME"].ToString();
                        string _formTitle = rowMenu["MENUFULLNAME"].ToString();
                        string _formname = rowMenu["formname"].ToString();
                        Code.StaticVariable.menuID_Active = menuID;
                        Code.StaticVariable.menu_Code = rowMenu["menucode"].ToString(); ;
                        Code.StaticVariable.formname_Active = function_name;
                        switch (_formname)
                        {
                            case "frmview":
                                Forms.FrmView _FrmView = new Forms.FrmView(function_name, _formTitle);
                                _FrmView.ShowDialog();
                                break;
                            case "frmphanbolenhmua":
                                Forms.Form1 _Frm_PhanBoLenhKHTC = new Forms.Form1();
                                _Frm_PhanBoLenhKHTC.ShowDialog();
                                break;
                            case "Frm_Group_Permission":
                                Forms.Frm_Group_Permission _form = new Forms.Frm_Group_Permission();
                                _form.ShowDialog();
                                break;
                            case "frmReport":
                                FrmMainReport _frmRPT = new FrmMainReport();
                                _frmRPT.ShowDialog();
                                break;
                            case "Frm_SoTraiChu":
                                Forms.SoTraiChu.Frm_SoTraiChu _Frm_SoTraiChu = new Forms.SoTraiChu.Frm_SoTraiChu();
                                _Frm_SoTraiChu.ShowDialog();
                                break;
                            case "Frm_ThanhToanLaiGoc":
                                Forms.SoTraiChu.Frm_ThanhToanLaiGoc _Frm_ThanhToanLaiGoc = new Forms.SoTraiChu.Frm_ThanhToanLaiGoc();
                                _Frm_ThanhToanLaiGoc.ShowDialog();
                                break;
                            case "Frm_ChuyenNhuong":
                                Forms.SoTraiChu.Frm_ChuyenNhuong _Frm_ChuyenNhuong = new Forms.SoTraiChu.Frm_ChuyenNhuong();
                                _Frm_ChuyenNhuong.ShowDialog();
                                break;
                            case "Frm_PhongToa":
                                Forms.SoTraiChu.Frm_PhongToa _Frm_PhongToa = new Forms.SoTraiChu.Frm_PhongToa();
                                _Frm_PhongToa.ShowDialog();
                                break;
                            case "Frm_GiaiPhongToa":
                                Forms.SoTraiChu.Frm_GiaiPhongToa _Frm_GiaiPhongToa = new Forms.SoTraiChu.Frm_GiaiPhongToa();
                                _Frm_GiaiPhongToa.ShowDialog();
                                break;
                            case "Frm_CamCo":
                                Forms.SoTraiChu.Frm_CamCo _Frm_CamCo = new Forms.SoTraiChu.Frm_CamCo();
                                _Frm_CamCo.ShowDialog();
                                break;
                            case "Frm_GiaiCamCo":
                                Forms.SoTraiChu.Frm_GiaiCamCo _Frm_GiaiCamCo = new Forms.SoTraiChu.Frm_GiaiCamCo();
                                _Frm_GiaiCamCo.ShowDialog();
                                break;
                            case "Frm_MuaLai":
                                Forms.SoTraiChu.Frm_MuaLai _Frm_MuaLai = new Forms.SoTraiChu.Frm_MuaLai();
                                _Frm_MuaLai.ShowDialog();
                                break;
                            case "Frm_InSoTraiChu":
                                Forms.SoTraiChu.Frm_InSoTraiChu _Frm_InSoTraiChu = new Forms.SoTraiChu.Frm_InSoTraiChu();
                                _Frm_InSoTraiChu.ShowDialog();
                                break;
                            case "Frm_TraSo":
                                Forms.SoTraiChu.Frm_TraSo _Frm_TraSo = new Forms.SoTraiChu.Frm_TraSo();
                                _Frm_TraSo.ShowDialog();
                                break;
                            case "Frm_NopTien":
                                Forms.TKTienKH.Frm_NopTien _Frm_NopTien = new Forms.TKTienKH.Frm_NopTien();
                                _Frm_NopTien.ShowDialog();
                                break;
                            case "Frm_RutTien":
                                Forms.TKTienKH.Frm_RutTien _Frm_RutTien = new Forms.TKTienKH.Frm_RutTien();
                                _Frm_RutTien.ShowDialog();
                                break;
                            case "Frm_PhongToaTien":
                                Forms.TKTienKH.Frm_PhongToaTien _Frm_PhongToaTien = new Forms.TKTienKH.Frm_PhongToaTien();
                                _Frm_PhongToaTien.ShowDialog();
                                break;
                            case "Frm_GiaiToaTien":
                                Forms.TKTienKH.Frm_GiaiToaTien _Frm_GiaiToaTien = new Forms.TKTienKH.Frm_GiaiToaTien();
                                _Frm_GiaiToaTien.ShowDialog();
                                break;
                            default:
                                break;
                        }
                        this.txtMaGiaoDich.Clear();
                    }
                    else
                    {
                        MessageBox.Show(Code.StaticVariable.messeseboxMaGiaoDichSai,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        this.txtMaGiaoDich.Clear();
                    }
                }
            }
        }

        private DataTable tableCmdMenu(string _menucode)
        {
            DataTable result = _Services_BSC_OTC.GetMenu_ByMenuCode(_menucode);
            if (result.Rows.Count>0)
            {
                string menuid = Convert.ToString(result.Rows[0]["menuid"]);
                var results = from myrow in Code.StaticVariable.tablePermission.AsEnumerable()
                              where myrow.Field<string>("menuid") == menuid
                              select myrow;
                if (results.Count() > 0)
                {
                    result = results.CopyToDataTable();
                }
            }
            return result;
        }

        // Thoát khỏi ứng dụng
        void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void thoátKhỏiChươngTrìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn ra khỏi chương trình?","OTC Management",MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void about_Click(object sender, EventArgs e)
        {
            Forms.About frm_about = new Forms.About();
            frm_about.ShowDialog();
        }
        #endregion


    }
}